//
//  LocationViewController.m
//  huashiedu
//
//  Created by oceans on 2018/8/27.
//  Copyright © 2018年 hema. All rights reserved.
//

#import "LocationViewController.h"
#import "LocationDataModel.h"
#import "LocationCollectionViewCell.h"
#import "LocationItemCollectionViewCell.h"
#import "LocationHeaderReusableView.h"
#import "LocationTitleHeaderReusableView.h"
#import "LocationModel.h"

@interface LocationViewController () <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UISearchBarDelegate>

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UISearchBar *searchBar;
@property (strong, nonatomic) NSArray <NSArray *> *sections;
@property (strong, nonatomic) NSMutableArray *keysArr;
@end

@implementation LocationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setup];
}

- (void)setup {
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.keysArr = [NSMutableArray arrayWithObjects:@"当前定位：", @"热门城市", nil];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return _sections.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    
    if (section == 0) {
        return CGSizeMake(ScreenWidth, 45);
    }else if (section == 1) {
        return CGSizeMake(ScreenWidth, 44);
    }
    return CGSizeMake(ScreenWidth, 28);
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        
        if (indexPath.section == 0) {
            LocationHeaderReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"LocationHeaderReusableView" forIndexPath:indexPath];
//            view.delegate = self;
            view.textLabel.text = [NSString stringWithFormat:@"%@%@%@", _keysArr[indexPath.section], [LocationModel shared].cityName, [LocationModel shared].areaName];
            return view;
        }else {
            LocationTitleHeaderReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"LocationTitleHeaderReusableView" forIndexPath:indexPath];
            view.textLabel.text = _keysArr[indexPath.section];
            return view;
        }
    }
    return nil;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _sections[section].count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section < 2) {
        return CGSizeMake((ScreenWidth - 70), 40);
    }
    return CGSizeMake(ScreenWidth, 45);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section < 2) {
        LocationItemCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LocationItemCollectionViewCell" forIndexPath:indexPath];
        return cell;
    }else {
        LocationCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LocationCollectionViewCell" forIndexPath:indexPath];
        return cell;
    }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        
    }
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    
    if (section < 2) {
        return 10;
    }
    return 0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    if (section < 2) {
        return 21;
    }
    return 0;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    if (section == 0) {
        return UIEdgeInsetsMake(6, 14, 20, 14);
    }else if (section == 1) {
        return UIEdgeInsetsMake(0, 14, 10, 14);
    }
    return UIEdgeInsetsZero;
}



- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
    searchBar.showsCancelButton = YES;
    return YES;
}

- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar {
    searchBar.showsCancelButton = NO;
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)forBack:(UIButton *)sender {
    
}

@end
